/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.TrainQyty;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.service.TrainQytyService;
import me.zhengjie.domain.vo.TrainQytyQueryCriteria;
import me.zhengjie.mapper.TrainQytyMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author huangwencong
* @date 2024-10-07
**/
@Service
@RequiredArgsConstructor
public class TrainQytyServiceImpl extends ServiceImpl<TrainQytyMapper, TrainQyty> implements TrainQytyService {

    private final TrainQytyMapper trainQytyMapper;

    @Override
    public TrainQyty findOne(Long trainId, Long trainItemId) {
        return trainQytyMapper.findOne(trainId, trainItemId);
    }

    @Override
    public PageResult<TrainQyty> queryAll(TrainQytyQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(trainQytyMapper.findAll(criteria, page));
    }

    @Override
    public List<TrainQyty> queryAll(TrainQytyQueryCriteria criteria){
        return trainQytyMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(TrainQyty resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TrainQyty resources) {
        TrainQyty trainQyty = getById(resources.getId());
        trainQyty.copy(resources);
        saveOrUpdate(trainQyty);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<TrainQyty> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TrainQyty trainQyty : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("训练ID", trainQyty.getTrainId());
            map.put("训练前(血氧饱和|心率|呼吸|血压)", trainQyty.getXlq());
            map.put("7500m即刻(血氧饱和|心率|呼吸|血压)", trainQyty.getXljk());
            map.put("7500m 20min(血氧饱和|心率|呼吸|血压)", trainQyty.getXl20());
            map.put("7500m 40min(血氧饱和|心率|呼吸|血压)", trainQyty.getXl40());
            map.put("7500m 60min(血氧饱和|心率|呼吸|血压)", trainQyty.getXl60());
            map.put("7500m 80min(血氧饱和|心率|呼吸|血压)", trainQyty.getXl80());
            map.put("7500m 100min(血氧饱和|心率|呼吸|血压)", trainQyty.getXl100());
            map.put("7500m 120min(血氧饱和|心率|呼吸|血压)", trainQyty.getXl120());
            map.put("7500m 140min(血氧饱和|心率|呼吸|血压)", trainQyty.getXl140());
            map.put("7500m 160min(血氧饱和|心率|呼吸|血压)", trainQyty.getXl160());
            map.put("7500m 180min(血氧饱和|心率|呼吸|血压)", trainQyty.getXl180());
            map.put("7500m 200min(血氧饱和|心率|呼吸|血压)", trainQyty.getXl200());
            map.put("7500m 220min(血氧饱和|心率|呼吸|血压)", trainQyty.getXl220());
            map.put("7500m 240min(血氧饱和|心率|呼吸|血压)", trainQyty.getXl240());
            map.put("7500m 260min(血氧饱和|心率|呼吸|血压)", trainQyty.getXl260());
            map.put("7500m 280min(血氧饱和|心率|呼吸|血压)", trainQyty.getXl280());
            map.put("7500m 300min(血氧饱和|心率|呼吸|血压)", trainQyty.getXl300());
            map.put("7500m 320min(血氧饱和|心率|呼吸|血压)", trainQyty.getXl320());
            map.put("7500m 340min(血氧饱和|心率|呼吸|血压)", trainQyty.getXl340());
            map.put("7500m 360min(血氧饱和|心率|呼吸|血压)", trainQyty.getXl360());
            map.put("训练后(血氧饱和|心率|呼吸|血压)", trainQyty.getXlh());
            map.put("主观症状(症状id1|症状id2|...)", trainQyty.getFeels());
            map.put("结果判定", trainQyty.getResult());
            map.put("建议", trainQyty.getJy());
            map.put("记录日期", trainQyty.getDate());
            map.put("记录人", trainQyty.getCreateBy());
            map.put("创建时间", trainQyty.getCreateTime());
            map.put("更新时间", trainQyty.getUpdateTime());
            map.put("训练项目ID", trainQyty.getTrainItemId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}